//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "MarketModelFactory.h"
using namespace Cephei::QL::Models::Marketmodels;
#include <gen/QL/Models/Marketmodels/MarketModel.h>
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::CMarketModelFactory::CMarketModelFactory (boost::shared_ptr<QuantLib::MarketModelFactory>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelFactory = NULL;
#endif
	_ppMarketModelFactory = &childNative;
    
}
Cephei::QL::Models::Marketmodels::CMarketModelFactory::CMarketModelFactory (QuantLib::MarketModelFactory& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phMarketModelFactory = NULL;
#endif
	_ppMarketModelFactory = new boost::shared_ptr<QuantLib::MarketModelFactory> (&childNative);
    
    _MarketModelFactoryOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::CMarketModelFactory::CMarketModelFactory (CMarketModelFactory^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelFactory = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMarketModelFactory = new boost::shared_ptr<QuantLib::MarketModelFactory> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::CMarketModelFactory::CMarketModelFactory (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelFactory = NULL;
#endif
	if (!t->IsSubclassOf(CMarketModelFactory::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::CMarketModelFactory::CMarketModelFactory (QuantLib::Handle<QuantLib::MarketModelFactory>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMarketModelFactory = &childNative;
	_ppMarketModelFactory = &static_cast<boost::shared_ptr<QuantLib::MarketModelFactory>>(childNative.currentLink());
    
    _MarketModelFactoryOwner = owner;
}
Cephei::QL::Models::Marketmodels::CMarketModelFactory::CMarketModelFactory (QuantLib::Handle<QuantLib::MarketModelFactory> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMarketModelFactory = &childNative;
	_ppMarketModelFactory = &static_cast<boost::shared_ptr<QuantLib::MarketModelFactory>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::CMarketModelFactory::CMarketModelFactory (QuantLib::MarketModelFactory childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelFactory = NULL;
#endif
	_ppMarketModelFactory = new boost::shared_ptr<QuantLib::MarketModelFactory> (new QuantLib::MarketModelFactory (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::CMarketModelFactory::~CMarketModelFactory ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppMarketModelFactory != NULL)
    {
	    delete _ppMarketModelFactory;
        _ppMarketModelFactory = NULL;
    }
}
Cephei::QL::Models::Marketmodels::CMarketModelFactory::!CMarketModelFactory ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppMarketModelFactory != NULL)
    {
	    delete _ppMarketModelFactory;
    }
}
QuantLib::MarketModelFactory& Cephei::QL::Models::Marketmodels::CMarketModelFactory::GetReference ()
{
    if (_ppMarketModelFactory == NULL) throw REFNEW NativeNullException ();
	return **_ppMarketModelFactory;
}
boost::shared_ptr<QuantLib::MarketModelFactory>& Cephei::QL::Models::Marketmodels::CMarketModelFactory::GetShared ()
{
    if (_ppMarketModelFactory == NULL) throw REFNEW NativeNullException ();
	return *_ppMarketModelFactory;
}
QuantLib::MarketModelFactory* Cephei::QL::Models::Marketmodels::CMarketModelFactory::GetPointer ()
{
    if (_ppMarketModelFactory == NULL) throw REFNEW NativeNullException ();
	return &**_ppMarketModelFactory;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MarketModelFactory>& Cephei::QL::Models::Marketmodels::CMarketModelFactory::GetHandle ()
{
	if (_phMarketModelFactory == NULL)
	{
		_phMarketModelFactory = new Handle<QuantLib::MarketModelFactory> (*_ppMarketModelFactory);
	}
	return *_phMarketModelFactory;
}
#endif
bool Cephei::QL::Models::Marketmodels::CMarketModelFactory::HasNative () 
{
	return (_ppMarketModelFactory != NULL);
}

Cephei::QL::Models::Marketmodels::IMarketModel^ Cephei::QL::Models::Marketmodels::CMarketModelFactory::Create (Cephei::QL::Models::Marketmodels::IEvolutionDescription^ prm1, UInt64 numberOfFactors)
{
    CEvolutionDescription^ _Cprm1;
    try
    {
        _Cprm1 = safe_cast<CEvolutionDescription^> (prm1);
        _Cprm1->Lock();
        QuantLib::EvolutionDescription& _prm1 = static_cast<QuantLib::EvolutionDescription&> (_Cprm1->GetReference ()); 
        QuantLib::Size _numberOfFactors = (QuantLib::Size)ValueHelper::Convert (numberOfFactors); //a
    	boost::shared_ptr<QuantLib::MarketModel> _rv = (boost::shared_ptr<QuantLib::MarketModel>)(*_ppMarketModelFactory)->create ( _prm1,  _numberOfFactors );   
        Cephei::QL::Models::Marketmodels::CMarketModel^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::CMarketModel (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

